package com.github.catvod.spider.merge;

import com.github.catvod.spider.merge.HZ;
import com.github.catvod.spider.merge.ms;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

/* loaded from: classes.dex */
public class Lm extends fa {
    private iP BQ;

    @Nullable
    private QF Qy;

    @Nullable
    private WeakReference<List<Lm>> W;
    List<fa> u;
    private static final List<Lm> q = Collections.emptyList();
    private static final Pattern I = Pattern.compile("\\s+");
    private static final String oC = QF.j5("baseUri");

    class Tw implements aC {
        final /* synthetic */ StringBuilder h;

        Tw(StringBuilder sb) {
            this.h = sb;
        }

        @Override // com.github.catvod.spider.merge.aC
        public void V0(fa faVar, int i) {
            if (faVar instanceof IG) {
                Lm.ap(this.h, (IG) faVar);
            } else if (faVar instanceof Lm) {
                Lm lm = (Lm) faVar;
                if (this.h.length() > 0) {
                    if ((lm.rG() || lm.BQ.j().equals("br")) && !IG.SS(this.h)) {
                        this.h.append(' ');
                    }
                }
            }
        }

        @Override // com.github.catvod.spider.merge.aC
        public void h(fa faVar, int i) {
            if ((faVar instanceof Lm) && ((Lm) faVar).rG() && (faVar.GM() instanceof IG) && !IG.SS(this.h)) {
                this.h.append(' ');
            }
        }
    }

    private static final class yh extends hT<fa> {
        private final Lm h;

        yh(Lm lm, int i) {
            super(i);
            this.h = lm;
        }

        @Override // com.github.catvod.spider.merge.hT
        public void h() {
            this.h.yx();
        }
    }

    public Lm(iP iPVar, @Nullable String str, @Nullable QF qf) {
        R2.u(iPVar);
        this.u = fa.h;
        this.Qy = qf;
        this.BQ = iPVar;
        if (str != null) {
            F(str);
        }
    }

    private static String Gg(Lm lm, String str) {
        while (lm != null) {
            QF qf = lm.Qy;
            if (qf != null && qf.t(str)) {
                return lm.Qy.oq(str);
            }
            lm = lm.C4();
        }
        return "";
    }

    private static void M(Lm lm, StringBuilder sb) {
        if (!lm.BQ.j().equals("br") || IG.SS(sb)) {
            return;
        }
        sb.append(" ");
    }

    private boolean S(HZ.Tw tw) {
        return (!XA().BQ() || XA().I() || (C4() != null && !C4().rG()) || Ig() == null || tw.BQ()) ? false : true;
    }

    private static <E extends Lm> int Xu(Lm lm, List<E> list) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (list.get(i) == lm) {
                return i;
            }
        }
        return 0;
    }

    private boolean Y(HZ.Tw tw) {
        return this.BQ.V0() || (C4() != null && C4().XA().V0()) || tw.BQ();
    }

    private void Yd(StringBuilder sb) {
        for (fa faVar : this.u) {
            if (faVar instanceof IG) {
                ap(sb, (IG) faVar);
            } else if (faVar instanceof Lm) {
                M((Lm) faVar, sb);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void ap(StringBuilder sb, IG ig) {
        String M = ig.M();
        if (vm(ig.V0) || (ig instanceof ok)) {
            sb.append(M);
        } else {
            ZG.h(sb, M, IG.SS(sb));
        }
    }

    static boolean vm(@Nullable fa faVar) {
        if (faVar instanceof Lm) {
            Lm lm = (Lm) faVar;
            int i = 0;
            while (!lm.BQ.oq()) {
                lm = lm.C4();
                i++;
                if (i < 6 && lm != null) {
                }
            }
            return true;
        }
        return false;
    }

    @Nullable
    public Lm B() {
        List<Lm> i;
        int Xu;
        if (this.V0 != null && (Xu = Xu(this, (i = C4().i()))) > 0) {
            return i.get(Xu - 1);
        }
        return null;
    }

    @Override // com.github.catvod.spider.merge.fa
    /* renamed from: C, reason: merged with bridge method [inline-methods] */
    public Lm n() {
        this.u.clear();
        return this;
    }

    @Nullable
    public Lm DQ(String str) {
        return CR.j(str, this);
    }

    public Lm HU(String str, String str2) {
        super.q(str, str2);
        return this;
    }

    @Override // com.github.catvod.spider.merge.fa
    public QF I() {
        if (this.Qy == null) {
            this.Qy = new QF();
        }
        return this.Qy;
    }

    public List<IG> J() {
        ArrayList arrayList = new ArrayList();
        for (fa faVar : this.u) {
            if (faVar instanceof IG) {
                arrayList.add((IG) faVar);
            }
        }
        return Collections.unmodifiableList(arrayList);
    }

    public Dv K() {
        if (this.V0 == null) {
            return new Dv(0);
        }
        List<Lm> i = C4().i();
        Dv dv = new Dv(i.size() - 1);
        for (Lm lm : i) {
            if (lm != this) {
                dv.add(lm);
            }
        }
        return dv;
    }

    public String MC() {
        return this.BQ.j();
    }

    public int N() {
        if (C4() == null) {
            return 0;
        }
        return Xu(this, C4().i());
    }

    public Dv O() {
        return zt.h(new ms.Tw(), this);
    }

    @Override // com.github.catvod.spider.merge.fa
    void Og(Appendable appendable, int i, HZ.Tw tw) {
        if (tw.u() && Y(tw) && !S(tw)) {
            if (!(appendable instanceof StringBuilder)) {
                j5(appendable, i, tw);
            } else if (((StringBuilder) appendable).length() > 0) {
                j5(appendable, i, tw);
            }
        }
        appendable.append('<').append(MC());
        QF qf = this.Qy;
        if (qf != null) {
            qf.i4(appendable, tw);
        }
        if (!this.u.isEmpty() || !this.BQ.u()) {
            appendable.append('>');
        } else if (tw.Qy() == HZ.Tw.EnumC0010Tw.html && this.BQ.I()) {
            appendable.append('>');
        } else {
            appendable.append(" />");
        }
    }

    public String Oh() {
        StringBuilder V0 = ZG.V0();
        Yd(V0);
        return ZG.t(V0).trim();
    }

    @Override // com.github.catvod.spider.merge.fa
    /* renamed from: P, reason: merged with bridge method [inline-methods] */
    public Lm z() {
        return (Lm) super.z();
    }

    @Override // com.github.catvod.spider.merge.fa
    public String PU() {
        return this.BQ.j();
    }

    public Dv Pi(String str) {
        return CR.h(str, this);
    }

    @Override // com.github.catvod.spider.merge.fa
    void R(Appendable appendable, int i, HZ.Tw tw) {
        if (this.u.isEmpty() && this.BQ.u()) {
            return;
        }
        if (tw.u() && !this.u.isEmpty() && (this.BQ.V0() || (tw.BQ() && (this.u.size() > 1 || (this.u.size() == 1 && !(this.u.get(0) instanceof IG)))))) {
            j5(appendable, i, tw);
        }
        appendable.append("</").append(MC()).append('>');
    }

    public Lm SS(fa faVar) {
        return (Lm) super.BQ(faVar);
    }

    public iP XA() {
        return this.BQ;
    }

    public String a() {
        QF qf = this.Qy;
        return qf != null ? qf.F8("id") : "";
    }

    public Lm d1(int i) {
        return i().get(i);
    }

    public String e() {
        StringBuilder V0 = ZG.V0();
        bY.V0(new Tw(V0), this);
        return ZG.t(V0).trim();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.github.catvod.spider.merge.fa
    /* renamed from: fR, reason: merged with bridge method [inline-methods] */
    public Lm F8(@Nullable fa faVar) {
        Lm lm = (Lm) super.F8(faVar);
        QF qf = this.Qy;
        lm.Qy = qf != null ? qf.clone() : null;
        yh yhVar = new yh(lm, this.u.size());
        lm.u = yhVar;
        yhVar.addAll(this.u);
        return lm;
    }

    public String g6() {
        StringBuilder V0 = ZG.V0();
        for (fa faVar : this.u) {
            if (faVar instanceof BG) {
                V0.append(((BG) faVar).M());
            } else if (faVar instanceof mF) {
                V0.append(((mF) faVar).M());
            } else if (faVar instanceof Lm) {
                V0.append(((Lm) faVar).g6());
            } else if (faVar instanceof ok) {
                V0.append(((ok) faVar).M());
            }
        }
        return ZG.t(V0);
    }

    public boolean hP(String str) {
        QF qf = this.Qy;
        if (qf == null) {
            return false;
        }
        String F8 = qf.F8("class");
        int length = F8.length();
        int length2 = str.length();
        if (length != 0 && length >= length2) {
            if (length == length2) {
                return str.equalsIgnoreCase(F8);
            }
            boolean z = false;
            int i = 0;
            for (int i2 = 0; i2 < length; i2++) {
                if (Character.isWhitespace(F8.charAt(i2))) {
                    if (!z) {
                        continue;
                    } else {
                        if (i2 - i == length2 && F8.regionMatches(true, i, str, 0, length2)) {
                            return true;
                        }
                        z = false;
                    }
                } else if (!z) {
                    i = i2;
                    z = true;
                }
            }
            if (z && length - i == length2) {
                return F8.regionMatches(true, i, str, 0, length2);
            }
        }
        return false;
    }

    List<Lm> i() {
        List<Lm> list;
        if (u() == 0) {
            return q;
        }
        WeakReference<List<Lm>> weakReference = this.W;
        if (weakReference != null && (list = weakReference.get()) != null) {
            return list;
        }
        int size = this.u.size();
        ArrayList arrayList = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            fa faVar = this.u.get(i);
            if (faVar instanceof Lm) {
                arrayList.add((Lm) faVar);
            }
        }
        this.W = new WeakReference<>(arrayList);
        return arrayList;
    }

    @Override // com.github.catvod.spider.merge.fa
    protected List<fa> iU() {
        if (this.u == fa.h) {
            this.u = new yh(this, 4);
        }
        return this.u;
    }

    @Override // com.github.catvod.spider.merge.fa
    /* renamed from: lu, reason: merged with bridge method [inline-methods] */
    public final Lm C4() {
        return (Lm) this.V0;
    }

    public <T extends Appendable> T nO(T t) {
        int size = this.u.size();
        for (int i = 0; i < size; i++) {
            this.u.get(i).LW(t);
        }
        return t;
    }

    @Override // com.github.catvod.spider.merge.fa
    public String oC() {
        return Gg(this, oC);
    }

    @Override // com.github.catvod.spider.merge.fa
    /* renamed from: r4, reason: merged with bridge method [inline-methods] */
    public Lm oq() {
        return (Lm) super.oq();
    }

    public boolean rG() {
        return this.BQ.q();
    }

    public Lm rl(fa faVar) {
        R2.u(faVar);
        T(faVar);
        iU();
        this.u.add(faVar);
        faVar.FE(this.u.size() - 1);
        return this;
    }

    public Dv s() {
        return new Dv(i());
    }

    @Override // com.github.catvod.spider.merge.fa
    protected void t(String str) {
        I().yx(oC, str);
    }

    @Override // com.github.catvod.spider.merge.fa
    public int u() {
        return this.u.size();
    }

    public String w() {
        return this.BQ.Qy();
    }

    @Override // com.github.catvod.spider.merge.fa
    protected boolean y() {
        return this.Qy != null;
    }

    public String yp() {
        StringBuilder V0 = ZG.V0();
        nO(V0);
        String t = ZG.t(V0);
        return n.h(this).u() ? t.trim() : t;
    }

    @Override // com.github.catvod.spider.merge.fa
    void yx() {
        super.yx();
        this.W = null;
    }

    public Lm(iP iPVar, String str) {
        this(iPVar, str, null);
    }
}
